<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #app {
            width: 300px;
            height: 300px;
            background-color: aqua;
        }

        .sonbg {
            width: 120px;
            height: 80px;
            background-color: chartreuse;
        }
    </style>
</head>

<body>
    <div id="app">
        {{fmsg}}
        <my-son @myclick="fn"></my-son>
    </div>
</body>

</html>
<script src="./vue.js"></script>
<script>
    /* 
    1.在父组件中使用子组件，在子组件标签上绑定一个自定义的myclick函数，值为函数名fn
    2.在子组件内可以使用 this.$emit()语法来触发父组件的自定义事件，从而让fn函数执行
    */

    Vue.component("mySon", {
        template: `<div class="sonbg">
            <button @click="ziMethod">点我触发父组件myclick事件</button>
            </div>`,
        data() {
            return {
                msg: "子组件的内容",
                count: 10
            }
        },
        methods: {
            ziMethod() {
                this.$emit("myclick", this.msg)
            }
        }
    })

    const vm = new Vue({
        el: "#app",
        data: {
            fmsg: ''
        },
        methods: {
            fn(x) {
                this.fmsg = x
            }
        }
    })

</script>